{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 分组",
   "id": "d991fc9642c21cb4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T09:16:46.886047Z",
     "start_time": "2025-09-15T09:16:46.883487Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day16/file/'"
   ],
   "id": "cc160f88093f032d",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T09:32:25.410920Z",
     "start_time": "2025-09-15T09:32:25.391937Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 按索引奇偶分组\n",
    "df = pd.read_excel(path + '分组.xlsx')\n",
    "df2 = df.groupby(df.index % 2 == 0)\n",
    "# for item in df2:\n",
    "#     print(item)\n",
    "# print(df2[['语文','数学','英语']].sum()) # 奇数组和偶数组各门课的总成绩\n",
    "\n",
    "# 2. 按前后5行分组\n",
    "df3 = df.groupby(df.index >= 5)\n",
    "# print(df3.apply(lambda x:x))\n",
    "# for item in df3:\n",
    "#     print(item)\n",
    "\n",
    "# 3. 按姓氏首字母分组\n",
    "df4 = df.groupby(df['姓名'].str[0])\n",
    "# print(df4.apply(lambda x:x))\n",
    "\n",
    "# 4. 按姓名前两个字分组\n",
    "df5 = df.groupby([df['姓名'].str[0],df['姓名'].str[1]])\n",
    "# print(df5.apply(lambda x: x))\n",
    "\n",
    "# 5. 按指定班级分组（包含）\n",
    "df6 = df.groupby(df['班级'].isin(['1班','2班']))\n",
    "# print(df6.apply(lambda x: x))\n",
    "\n",
    "# 6. 按非指定班级分组（排除）\n",
    "df7 = df.groupby(~df['班级'].isin(['1班','2班']))\n",
    "# print(df7.apply(lambda x: x))\n",
    "\n",
    "# 7. 按日期和小时分组\n",
    "df8 = df.groupby([df['时间'].dt.date,df['时间'].dt.hour])\n",
    "# print(df8.apply(lambda x: x))\n",
    "\n",
    "\n",
    "\n",
    "# 8. 按年份分组\n",
    "df9 = df.groupby(df['时间'].dt.year)\n",
    "# print(df9.apply(lambda x: x))\n",
    "\n",
    "# 9. 使用 pipe 进行分组聚合\n",
    "df9 = df.pipe(lambda x:x.groupby('班级'))\n",
    "print(df9.apply(lambda x: x))\n"
   ],
   "id": "9988a690132339c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        学号                  时间   姓名  班级 性别  数学  语文  英语\n",
      "班级                                                    \n",
      "1班 0  A001 2024-05-01 17:00:00  丁智敏  1班  女  54  34  51\n",
      "   6  A007 2025-05-01 08:00:00  李永兴  1班  男  43  53  42\n",
      "   9  A010 2024-05-02 18:00:00  张荣耀  1班  男  67  65  36\n",
      "2班 2  A003 2024-05-02 09:00:00   张伊  2班  女  32  65  40\n",
      "   5  A006 2024-05-02 18:00:00  卢海军  2班  男  56  40  39\n",
      "3班 1  A002 2024-05-02 18:00:00  李平平  3班  女  68  48  41\n",
      "   4  A005 2024-05-01 17:00:00   王松  3班  男  59  55  39\n",
      "   8  A009 2024-05-01 17:00:00   李超  3班  男  38  35  51\n",
      "4班 3  A004 2024-05-01 08:00:00   王刚  4班  男  70  50  37\n",
      "   7  A008 2024-05-02 09:00:00   王硕  4班  女  68  68  69\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\16357\\AppData\\Local\\Temp\\ipykernel_7508\\2067033325.py:42: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  print(df9.apply(lambda x: x))\n"
     ]
    }
   ],
   "execution_count": 27
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
